package m3;

import com.rabbitmq.client.BuiltinExchangeType;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.ConnectionFactory;

import java.io.IOException;
import java.util.Scanner;
import java.util.concurrent.TimeoutException;

public class Producer {
    public static void main(String[] args) throws IOException, TimeoutException {
        //连接
        ConnectionFactory f = new ConnectionFactory();
        f.setHost("192.168.64.140");
        f.setUsername("admin");
        f.setPassword("admin");
        Channel c = f.newConnection().createChannel();
        //创建Fanout 类型的交换机 ：  Logs
        //c.exchangeDeclare("logs", "fanout");
        c.exchangeDeclare("logs", BuiltinExchangeType.FANOUT);



        //向logs 交换机来发送消息
        while (true){
            System.out.println("输入消息");
            String s = new Scanner(System.in).nextLine();
            //第二个参数对于交换机来说是无效的。因为没有消息队列，如果没有人接收消息的话，消息自动丢弃
            c.basicPublish("logs", "", null,s.getBytes() );
        }
    }
}
